﻿using SLTimeSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SLBehaviourSystem
{
    [ActionComponent]
    public class WaitComponent : BehaviourComponent
    {
        float waitTime;
        TimeAgent timer;

        Action<BehaviourNodeState> OnExcuteCompleted;
        protected override void OnBindNode()
        {
            base.OnBindNode();
            waitTime = node.BehaviourData.GetFloat("WaitTime");
        }

        public override BehaviourNodeState Excute()
        {
            return BehaviourNodeState.Running;
        }

        public override void ExcuteAsync(Action<BehaviourNodeState> onExcuteCompleted)
        {
            OnExcuteCompleted = onExcuteCompleted;
            if (timer == null)
            {
                timer = TimeSystem.Inst.RegisterTimer(waitTime, 1, () =>
                {
                    OnExcuteCompleted(BehaviourNodeState.Success);
                });
            }
            else
            {
                timer.SetTime(waitTime);
                timer.Reset();
                timer.SetEnable(true);
            }
        }

        public override void Reset()
        {
            if (timer != null)
            {
                timer.Reset();
                timer.SetEnable(false);
            }
        }
    }
}
